/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hadss_uni_input/hadss_uni_input.dart';

void main() {
  /// 测试鼠标右键触发上下文菜单
  testWidgets('secondaryMouseButton onContentMenu test',
      (WidgetTester tester) async {
    bool isOnContentMenu = false;
    await tester.pumpWidget(MaterialApp(
      home: UnifiedGestureDetector(
        pointerOptions: const PointerOptions(),
        onContentMenu: (event) {
          isOnContentMenu = true;
        },
        child: ListView(
          children: const [
            SizedBox(height: 1000, child: Text('onContentMenu'))
          ],
        ),
      ),
    ));

    try {
      await tester.tap(find.byType(ListView),
          pointer: 1,
          buttons: kSecondaryMouseButton,
          warnIfMissed: true,
          kind: PointerDeviceKind.mouse);

      // 等待动画完成
      await tester.pumpAndSettle();

      // 验证是否触发上下文菜单
      expect(isOnContentMenu, true);
    } catch (e) {
      // 捕获所有其他异常
      if (kDebugMode) {
        print('错误');
      }
    }
  });

  /// 测试滚动平移
  testWidgets('pan test', (WidgetTester tester) async {
    double offsetX = 0.0;
    double offsetY = 0.0;
    await tester.pumpWidget(MaterialApp(
      home: UnifiedGestureDetector(
        pointerOptions: const PointerOptions(
          enablePan: true,
        ),
        onPanUpdate: (event) {
          offsetX = event.offsetX ?? 0.0;
          offsetY = event.offsetY ?? 0.0;
        },
        child: ListView(
          children: const [SizedBox(height: 1000, child: Text('pan me'))],
        ),
      ),
    ));

    try {
      await tester.fling(find.byType(ListView), const Offset(0, -200), 1000.0);
      // 等待动画完成
      await tester.pumpAndSettle();
      // 验证拖动距离
      expect(offsetX.abs() + offsetY.abs(), greaterThan(0.0));
    } catch (e) {
      // 捕获所有其他异常
      if (kDebugMode) {
        print('错误');
      }
    }
  });

  /// 测试轻扫
  testWidgets('pan test', (WidgetTester tester) async {
    double angle = 0.0;
    await tester.pumpWidget(MaterialApp(
      home: UnifiedGestureDetector(
        pointerOptions: const PointerOptions(
          enableSwipe: true,
        ),
        onSwipe: (event) {
          angle = event.angle ?? 0.0;
        },
        child: Center(
          child: Transform(
            alignment: Alignment.center,
            transform: Matrix4.identity()..rotateZ(angle),
            child: Container(
              width: 200,
              height: 200,
              color: Colors.green,
              alignment: Alignment.center,
              child: const Text(
                '轻扫',
                style: TextStyle(fontSize: 30),
              ),
            ),
          ),
        ),
      ),
    ));

    try {
      await tester.fling(
          find.byType(Container), const Offset(120, -200), 100.0);
      // 等待动画完成
      await tester.pumpAndSettle();
      // 验证轻扫偏转角度
      expect(angle.abs(), greaterThan(0.0));
    } catch (e) {
      // 捕获所有其他异常
      if (kDebugMode) {
        print('错误');
      }
    }
  });
}
